/**
 * 
 */
package com.my.test.testevent;

import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * create date: 2010-10-19 ����01:32:37
 * @author Angela Tao
 * @version 1.0
 *
 */
public class GoodLucky implements ActionListener{
	
	private Frame f = new Frame("GoodLucky");
	private TextField tf = new TextField();
	private Button b1 = new Button("��ʼ");
	private Button b2 = new Button("ֹͣ");
	private Button b3 = new Button("���");
	private Panel p = new Panel();
	private boolean flag = false;

	/**
	 * 
	 */
	public GoodLucky() {
		// TODO Auto-generated constructor stub
		tf.setEditable(false);
		f.add(tf,"North");
		p.add(b1);
		p.add(b2);
		p.add(b3);
		f.add(p,"South");
		
		f.setLocation(200, 200);
		f.setSize(300, 200);
		f.setVisible(true);
		
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		
		b2.setEnabled(false);
		b3.setEnabled(false);
		
		b1.setActionCommand("start");
		b2.setActionCommand("stop");
		b3.setActionCommand("clear");
		
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		
		this.getNum();
	}
	
	/**
	 * ��ȡ7�������1-36�������뵽�ı�����
	 */
	public void getNum(){
		while(true){
			if(flag){
				String s = "";
				for(int i = 0; i < 7; i++){
					int m = (int)(Math.random()*36)+1;
					s += "   "+String.valueOf(m);
				}
				try {
					Thread.sleep(60);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				tf.setText(s);		
			}		
		}	
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new GoodLucky();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String s = arg0.getActionCommand();
		if("start".equals(s)){
			flag = true;
			b1.setEnabled(false);
			b2.setEnabled(true);
			b3.setEnabled(false);
		}else if("stop".equals(s)){
			flag = false;
			b1.setEnabled(true);
			b2.setEnabled(false);
			b3.setEnabled(true);
		}else{
			tf.setText("");
			b3.setEnabled(false);
		}
	}

}
